//
//  MyWindowTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/8.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MyWindowTableViewCell.h"
#import "GXMacro.h"
#import "ModelOfMineWindow.h"
#import "MyWindowCollectionViewCell.h"
@interface MyWindowTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation MyWindowTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.bounces = NO;
        [self.contentView addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[MyWindowCollectionViewCell class] forCellWithReuseIdentifier:@"MyWindowCollectionViewCell"];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(200, self.contentView.bounds.size.height - 40);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfWindowl.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyWindowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyWindowCollectionViewCell" forIndexPath:indexPath];
    ModelOfMineWindow *model = [_arrayOfWindowl objectAtIndex:indexPath.row];
    cell.labelOfWindowName.text = model.favorites_class_name;
    cell.labelOfMomentCount.text = [NSString stringWithFormat:@"共%@个瞬间", model.micro_personal_count];
    NSArray *arr = model.micro_personal;
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        [array addObject:dic[@"commend_image"]];
    }
    if (array.count >= 1) {
        [cell.image1 sd_setImageWithURL:[NSURL URLWithString:array[0]]];
    }
    if (array.count >= 2) {
        [cell.image1 sd_setImageWithURL:[NSURL URLWithString:array[0]]];

        [cell.image2 sd_setImageWithURL:[NSURL URLWithString:array[1]]];
    }
    if (array.count >= 3) {
        [cell.image1 sd_setImageWithURL:[NSURL URLWithString:array[0]]];
        
        [cell.image2 sd_setImageWithURL:[NSURL URLWithString:array[1]]];
        
        [cell.image3 sd_setImageWithURL:[NSURL URLWithString:array[2]]];
    }
    if (array.count >= 4) {
        [cell.image1 sd_setImageWithURL:[NSURL URLWithString:array[0]]];
        
        [cell.image2 sd_setImageWithURL:[NSURL URLWithString:array[1]]];
        
        [cell.image3 sd_setImageWithURL:[NSURL URLWithString:array[2]]];
        
        [cell.image4 sd_setImageWithURL:[NSURL URLWithString:array[3]]];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfMineWindow *model = [_arrayOfWindowl objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(myWindowWithID:)]) {
        [self.delegate myWindowWithID:model.favorites_class_id];
    }
}
- (void)setArrayOfWindowl:(NSArray *)arrayOfWindowl
{
    if (arrayOfWindowl.count != 0) {
        _arrayOfWindowl = arrayOfWindowl;
    }
    [_collectionView reloadData];
}
@end
